<?php
class ImagesHelper extends Helper
{
	
	function getResized($id, &$mime, $imgFolder, $newWidth=false, $newHeight=false, $bgcolor="000000", $resample=true, $cache=false, $cacheFolder=false, $cacheClear=false, $tempFolder=false)
	{
		$img = $imgFolder . $id;
		list($oldWidth, $oldHeight, $type) = getimagesize($img); 
		$ext = $this->image_type_to_extension($type);
		$mime = image_type_to_mime_type($type);

		if ($cache AND is_writeable($cacheFolder))
		{
			$dest = $cacheFolder . $id . '_' . $newWidth . 'x' . $newHeight;
		}
		else if (is_writeable($tempFolder))
		{
			$dest = $tempFolder . $id;
		}
		else
		{
			debug("You must set either a cache folder or temporal folder for image processing. And the folder has to be writable.");
			if (strlen($cacheFolder))
			{
				debug("Cache Folder \"".$cacheFolder."\" has permissions ".substr(sprintf('%o', fileperms($cacheFolder)), -4));
				debug("Please run \"chmod 777 $cacheFolder\"");
			}
			if (strlen($tempFolder))
			{
				debug("Temp Folder: ".$tempFolder." Permissions:".substr(sprintf('%o', fileperms($tempFolder)), -4));
				debug("Run \"chmod 777 $tempFolder\"");
			}
			exit();
		}
		
		if ($newWidth OR $newHeight)
		{
			if($cacheClear && file_exists($dest))
			{	
				unlink($dest);
			}
			
			if($cache AND file_exists($dest))
			{
				$i = fopen($dest, 'rb');
			}
			else
			{
				if(($newWidth > $oldWidth) && ($newHeight > $oldHeight)) 
				{
					$applyWidth = $oldWidth;
					$applyHeight = $oldHeight;
				} 
				else
				{
					if(($newWidth/$newHeight) < ($oldWidth/$oldHeight)) 
					{
						$applyHeight = $newWidth*$oldHeight/$oldWidth;
						$applyWidth = $newWidth;
					} 
					else
					{
						$applyWidth = $newHeight*$oldWidth/$oldHeight;
						$applyHeight = $newHeight;
					}
				}
				
				switch($ext)
				{
					case 'gif' :
						$oldImage = imagecreatefromgif($img);
						$newImage = imagecreate($newWidth, $newHeight);
						break;
					case 'png' :
						$oldImage = imagecreatefrompng($img);
						$newImage = imagecreatetruecolor($newWidth, $newHeight);
						break;
					case 'jpg' :
						$oldImage = imagecreatefromjpeg($img);
						$newImage = imagecreatetruecolor($newWidth, $newHeight);
						break;
					case 'jpeg' :
						$oldImage = imagecreatefromjpeg($img);
						$newImage = imagecreatetruecolor($newWidth, $newHeight);
						break;
					default :
						return false;
						break;
				}
				
				sscanf($bgcolor, "%2x%2x%2x", $red, $green, $blue);
				$newColor = ImageColorAllocate($newImage, $red, $green, $blue); 
				imagefill($newImage,0,0,$newColor);
				
				if ($resample==true)
				{
					imagecopyresampled($newImage, $oldImage, ($newWidth-$applyWidth)/2, ($newHeight-$applyHeight)/2, 0, 0, $applyWidth, $applyHeight, $oldWidth, $oldHeight);
				}
				else
				{
					imagecopyresized($newImage, $oldImage, ($newWidth-$applyWidth)/2, ($newHeight-$applyHeight)/2, 0, 0, $applyWidth, $applyHeight, $oldWidth, $oldHeight);
				}
	
				
					switch($ext)
					{
						case 'gif' :
							imagegif($newImage, $dest);
							break;
						case 'png' :
							imagepng($newImage, $dest);
							break;
						case 'jpg' :
							imagejpeg($newImage, $dest, '90');
							break;
						case 'jpeg' :
							imagejpeg($newImage, $dest);
							break;
						default :
							return false;
							break;
					}
				
				imagedestroy($newImage);
				imagedestroy($oldImage);
				$i = fopen($dest, 'rb');
			}

		}
		else
		{
			$i = fopen($img, 'rb');
		}
		
		return $i;
	}

	function image_type_to_extension($imagetype)
	{
	if(empty($imagetype)) return false;
		switch($imagetype)
		{
			case IMAGETYPE_GIF    : return 'gif';
			case IMAGETYPE_JPEG    : return 'jpg';
			case IMAGETYPE_PNG    : return 'png';
			case IMAGETYPE_SWF    : return 'swf';
			case IMAGETYPE_PSD    : return 'psd';
			case IMAGETYPE_BMP    : return 'bmp';
			case IMAGETYPE_TIFF_II : return 'tiff';
			case IMAGETYPE_TIFF_MM : return 'tiff';
			case IMAGETYPE_JPC    : return 'jpc';
			case IMAGETYPE_JP2    : return 'jp2';
			case IMAGETYPE_JPX    : return 'jpf';
			case IMAGETYPE_JB2    : return 'jb2';
			case IMAGETYPE_SWC    : return 'swc';
			case IMAGETYPE_IFF    : return 'aiff';
			case IMAGETYPE_WBMP    : return 'wbmp';
			case IMAGETYPE_XBM    : return 'xbm';
			default                : return false;
		}
	}
	
}
?>